package com.dankegongyu.lib.common.util;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;

import java.util.List;

import static android.content.Context.ACTIVITY_SERVICE;

/**
 * @author wupuquan
 * @version 1.0
 * @since 2018/6/1 11:37
 */
public class DKActivityUtil {

    private DKActivityUtil() {

    }

    /**
     * 判断某一个类是否存在任务栈里面
     */
    public static boolean isExistActivity(Context context, Class<?> cls) {
        try {
            Intent intent = new Intent(context, cls);
            ComponentName cmpName = intent.resolveActivity(context.getPackageManager());
            if (cmpName != null) { // 说明系统中存在这个activity
                ActivityManager am = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
                //noinspection ConstantConditions
                List<ActivityManager.RunningTaskInfo> taskInfoList = am.getRunningTasks(10);
                for (ActivityManager.RunningTaskInfo taskInfo : taskInfoList) {
                    if (taskInfo.baseActivity.equals(cmpName)) { // 说明它已经启动了
                        return true;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
